package com.log.james.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * IP的转变
 * @author jamesZhan
 */
public class IPChangeUtil {
    /**
     * ip(String)转Long
     * @return Long
     */
    public long ipStrToLong(String ipAddress){
        Long[] ip = new Long[4];
        int i = 0;
        for (String ipStr : ipAddress.split("\\.")){
            ip[i] = Long.parseLong(ipStr);
            i++;
        }
//        << 左移
        return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];
    }

    /**
     * IP Long 转 String
     * return String
     */
    public String ipLongToStr(long ipAddress){
        StringBuffer sb = new StringBuffer("");
        sb.append(String.valueOf(ipAddress >>> 24));
        sb.append(".");
        sb.append(String.valueOf((ipAddress & 0x00FFFFFF) >>> 16));
        sb.append(".");
        sb.append(String.valueOf((ipAddress & 0x0000FFFF) >>> 8));
        sb.append(".");
        sb.append(String.valueOf((ipAddress & 0x000000FF)));
        return sb.toString();
    }

    public static void main(String[] args) {
//        System.out.println(new IPChangeUtil().ipStrToLong("172.31.214.196"));
        System.out.println(new IPChangeUtil().ipLongToStr(Long.parseLong("3396061441")));
//        System.out.println(new IPChangeUtil().ipLongToStr(new IPChangeUtil().ipStrToLong("172.31.214.196")));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try{
//            Date date1 = sdf.parse("2016-02-19 00:05:00");
//            Date date2 = sdf.parse("2016-02-19 00:14:00");
            Date date1 = sdf.parse("2016-09-09 17:35:14");
            Date date2 = sdf.parse("2016-09-09 17:36:06");
            System.out.println(date1.getTime());
            System.out.println(date2.getTime());
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
